<div class="layui-field-box">
    <div class="layui-form-item" style="margin:0;margin-top:15px;">
        <div class="layui-inline">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline layui-short-input">
            </div>
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline layui-short-input">
                <input type="text" placeholder="" name="{{makeElUniqueName('goods_name')}}" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="width:auto">
                <button class="layui-btn" lay-filter="{{makeElUniqueName('search_goods')}}"><i class="layui-icon">&#xe615;</i> 搜索</button>&nbsp;
                <button class="layui-btn layui-btn-normal" lay-filter="{{makeElUniqueName('add_goods')}}"><i class="layui-icon">&#xe654; </i> 新增</button>
            </div>
        </div>
    </div>
</div>
<div id="{{makeElUniqueName('tbGoods')}}"></div>
<script>
    layui.use(['jfTable', 'validator'], function(){
        var layer = layui.layer;
        var $ = layui.jquery;
        var jfTable = layui.jfTable;

        layui.define(function(exports){
            var obj = {
                doEdit:function(roleId) {
                    $.get('/backstage/role/edit/'+ id, {}, function(str){
                        var popLayerUtil = layui.popLayerUtil;
                        popLayerUtil.doPopUp({
                            index: layer.open({
                                id: '{{makeElUniqueName('editGoods')}}',
                                title: '修改商品信息',
                                type: 1,
                                content: str,
                                area: ['500px', '330px']
                            }),
                            onClose: function() {
                                layui.roleFuncs.refreshTableGrid();
                            }
                        });
                    });
                },

                doDelete:function(id) {
                    layer.confirm('确定删除该商品？', {
                        btn: ['确定','放弃'],
                        icon: 3
                    }, function(){
                        var index = layer.load(1);
                        $.ajax({
                            contentType: "application/json",
                            type: 'post',
                            url: '/backstage/api/goods/delete',
                            data: JSON.stringify({
                                id: roleId
                            }),
                            success: function (outResult) {
                                layer.close(index);
                                if (outResult.Success) {
                                    layer.msg(outResult.Message, { icon: 6 });
                                    layui.roleFuncs.refreshTableGrid();
                                } else {
                                    layer.msg(outResult.Message, { icon: 5 });
                                }
                            },
                            error: function (error) {
                                layer.close(index);
                                layui.validator.processValidateError(error);
                            }
                        });
                    }, function(){
                    });
                },
                refreshTableGrid: function() {
                    $('input[name=\'{{makeElUniqueName('goods_name')}}\']').val('');
                    $("#{{makeElUniqueName('tbGoods')}}").jfTable("reload");
                }
            };
            exports('goodsFuncs', obj);
        });

        $("#{{makeElUniqueName('tbGoods')}}").jfTable({
            url: '/backstage/api/goods/query',
            pageSize:5,
            page: true,
            skip: true,
            first:'首页',
            last:'尾页',
            columns: [{
                text:'操作',
                name: 'id',
                width: 350,
                align: 'center',
                formatter: function(value, dataItem, index) {
                    var html = '<a class="layui-btn layui-btn-small layui-btn-normal" onclick="layui.goodsFuncs.doEdit(' + value + ')"><i class="layui-icon">&#xe642;</i> 编辑</a>';
                    html += '&nbsp;&nbsp;<a class="layui-btn layui-btn-small layui-btn-danger" onclick="layui.goodsFuncs.doDelete(' + value + ')"><i class="layui-icon">&#xe640;</i> 删除</a>';;
                    return html;
                }
            },{
                text:'商品名称',
                name: 'name',
                width: 160,
                align: 'center',
            },{
                text:'商品数量',
                name: 'number',
                width: 460,
                align: 'left',
            },{
                text:'商品价格',
                name: 'price',
                width: 460,
                align: 'left',
            },{
                text:'商品品牌',
                name: 'band_name',
                width: 460,
                align: 'left',
            }],
            method: 'get',
            queryParam: {
                name:$('input[name=\'{{makeElUniqueName('goods_name')}}\']').val()
            },
            toolbarClass: 'layui-btn-small',
            onBeforeLoad: function (param) {
                return $.extend(param, {
                    name:$('input[name=\'{{makeElUniqueName('goods_name')}}\']').val()
                });
            },
            onLoadSuccess: function (data) {
                return data;
            },
            dataFilter:function (data) {
                return data;
            }
        });

        $('.layui-btn[lay-filter=\'{{makeElUniqueName('search_goods')}}\']').on('click', function(){
            $("#{{makeElUniqueName('tbGoods')}}").jfTable("reload");
        });

        $('.layui-btn[lay-filter=\'{{makeElUniqueName('add_goods')}}\']').on('click', function(){
            $.get('/backstage/goods/create', {}, function(str){
                var popLayerUtil = layui.popLayerUtil;
                popLayerUtil.doPopUp({
                    index: layer.open({
                        id: '{{makeElUniqueName('createGoods')}}',
                        title: '新增',
                        type: 1,
                        content: str,
                        area: ['500px', '330px']
                    }),
                    onClose: function() {
                        layui.roleFuncs.refreshTableGrid();
                    }
                });
            });
        });
    });
</script>